const { read, write } = require('../lib/read&write')

const TABURL = '../data/tab.json'

function getAllTabs() {
  const __tabs = read(TABURL)
  return __tabs
}

function addTab(tab) {
  const __tabs = read(TABURL)
  __tabs.push(tab)
  write(__tabs, TABURL)
}


function editTab(tab) {
  const __tabs = read(TABURL)
  __tabs.splice(
    __tabs.findIndex((item) => item.lable === tab.lable),
    1,
    tab
  )
  write(__tabs, TABURL)
}

function removeTab(tab) {
  const __tabs = read(TABURL)
  const newTabs = __tabs.filter((item) => item.lable !== tab.lable)
  write(newTabs, TABURL)
}

function searchTab(tabLable) {
  const __Report = read(TABURL)
  return __Report.find((item) => item.lable === tabLable)
}

module.exports = {
  getAllTabs,
  addTab,
  editTab,
  searchTab,
  removeTab
}